import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.*;
import org.apache.ibatis.parsing.XNode;
import org.apache.ibatis.parsing.XPathParser;
import org.apache.ibatis.scripting.defaults.RawSqlSource;
import org.apache.ibatis.scripting.xmltags.StaticTextSqlNode;
import org.apache.ibatis.session.*;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.apache.ibatis.type.StringTypeHandler;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author cxx
 * @create 2020-05-29 15:32
 **/
public class Main {


    public static void main(String[] args) throws IOException {
        String resource = "test.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        XPathParser xPathParser = new XPathParser(inputStream);
        XNode user = xPathParser.evalNode ("/user");
        System.out.println (user.evalString ("name"));
        System.out.println (user.evalString ("age"));
    }

    public static void main3(String[] args) throws Exception {

        String resource = "mybatis.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder ();
        SqlSessionFactory sqlSessionFactory = ssfb.build (inputStream);
        SqlSession session = sqlSessionFactory.openSession ();
        List<Map> list = session.selectList ("userMapper.findUsers");
        System.out.println (list);
    }

    public static void main2(String[] args) {
        Configuration configuration = new Configuration ();
        RouteDataSource routeDataSource = new RouteDataSource ();

        PooledDataSource source1 = new PooledDataSource ();
        source1.setDriver ("com.mysql.jdbc.Driver");
        source1.setUrl ("jdbc:mysql://localhost:3306/test");
        source1.setUsername ("root");
        source1.setPassword ("123");

        PooledDataSource source2 = new PooledDataSource ();
        source2.setDriver ("com.mysql.jdbc.Driver");
        source2.setUrl ("jdbc:mysql://localhost:3306/test1");
        source2.setUsername ("root");
        source2.setPassword ("123");

        routeDataSource.setDefaultDataSource (source1);
        routeDataSource.addDataSource ("ds1",source1);
        routeDataSource.addDataSource ("ds2",source2);

        TransactionFactory transactionFactory = new JdbcTransactionFactory ();
        transactionFactory.newTransaction (routeDataSource, TransactionIsolationLevel.REPEATABLE_READ,true);
        Environment environment = new Environment ("test",transactionFactory,routeDataSource);
        configuration.setEnvironment (environment);

        configuration.addInterceptor (new RouteInterceptor ());

        StaticTextSqlNode sqlNode = new StaticTextSqlNode ("select * from user");
        RawSqlSource sqlSource = new RawSqlSource (configuration, sqlNode,Object.class);
        List<ResultMap> resultMaps = new ArrayList<> ();
        List<ResultMapping> mappings = new ArrayList<> ();
        ResultMap resultMap = new ResultMap.Builder(configuration,"rmap",Map.class,mappings).build ();
        resultMaps.add (resultMap);
        ResultMapping idMapping = new ResultMapping.Builder (configuration, "id").column ("id").typeHandler (new StringTypeHandler ()).build();
        ResultMapping nameMapping = new ResultMapping.Builder (configuration, "user_name").column ("user_name").typeHandler (new StringTypeHandler ()).build ();
        mappings.add (idMapping);
        mappings.add (nameMapping);
        MappedStatement ms = new MappedStatement.Builder (configuration, "t", sqlSource, SqlCommandType.SELECT)
                .resultMaps (resultMaps).build ();
        configuration.addMappedStatement (ms);
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder ();
        SqlSessionFactory sessionFactory = builder.build (configuration);
        SqlSession sqlSession = sessionFactory.openSession ();
        List<Map> list = sqlSession.selectList ("t");
        System.out.println (list);

    }

    public static void main1(String[] args) throws SQLException {

        Configuration configuration = new Configuration ();
        TransactionFactory transactionFactory = new JdbcTransactionFactory ();
        PooledDataSource dataSource =  new PooledDataSource ();
        dataSource.setUrl ("jdbc:mysql://localhost:3306/test");
        dataSource.setUsername ("root");
        dataSource.setPassword ("123");
        dataSource.setDriver ("com.mysql.jdbc.Driver");
        transactionFactory.newTransaction (dataSource, TransactionIsolationLevel.REPEATABLE_READ,true);
        Environment environment = new Environment ("test",transactionFactory,dataSource);
        configuration.setEnvironment (environment);
        StaticTextSqlNode sqlNode = new StaticTextSqlNode ("select * from user");
        RawSqlSource sqlSource = new RawSqlSource (configuration, sqlNode,Object.class);
        List<ResultMap> resultMaps = new ArrayList<> ();
        List<ResultMapping> mappings = new ArrayList<> ();
        ResultMap resultMap = new ResultMap.Builder(configuration,"rmap",Map.class,mappings).build ();
        resultMaps.add (resultMap);
        ResultMapping idMapping = new ResultMapping.Builder (configuration, "id").column ("id").typeHandler (new StringTypeHandler ()).build();
        ResultMapping nameMapping = new ResultMapping.Builder (configuration, "user_name").column ("user_name").typeHandler (new StringTypeHandler ()).build ();
        mappings.add (idMapping);
        mappings.add (nameMapping);
        MappedStatement ms = new MappedStatement.Builder (configuration, "t", sqlSource, SqlCommandType.SELECT)
                .resultMaps (resultMaps).build ();
        configuration.addMappedStatement (ms);
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder ();
        SqlSessionFactory sessionFactory = builder.build (configuration);
        SqlSession sqlSession = sessionFactory.openSession ();
        List<Map> list = sqlSession.selectList ("t");
        System.out.println (list);
    }

}
